package com.xq.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/18 14:23
 * @description druid自动配置类
 */
//@Configuration
//@ConditionalOnClass(value = {DruidDataSource.class})
//@EnableConfigurationProperties(DruidProperties.class)
public class DruidAutoConfiguration {
    /**
     * 创建日志对象
     */
    private Log log = LogFactory.getLog(DruidAutoConfiguration.class);

    @Autowired
    private DruidProperties druidProperties;

    /**
     * 创建druid数据源
     * @return
     */
    @Bean(initMethod = "init")
    public DruidDataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        // 数据库的URL为空则报错
        if (null == druidProperties.getUrl()) {
            log.error("url can not be null！");
            throw new RuntimeException("url can not be null！");
        }
        druidDataSource.setDriverClassName(druidProperties.getDriverClassName());
        druidDataSource.setUrl(druidDataSource.getUrl());
        druidDataSource.setUsername(druidDataSource.getUsername());
        druidDataSource.setMaxActive(druidDataSource.getMaxActive());
        druidDataSource.setMinIdle(druidDataSource.getMinIdle());
        druidDataSource.setMaxIdle(druidDataSource.getMaxIdle());
        druidDataSource.setInitialSize(druidDataSource.getInitialSize());
        druidDataSource.setValidationQuery(druidDataSource.getValidationQuery());

        return druidDataSource;
    }

    /**
     * 注册监听页面的Servlet
     * @return
     */
    @Bean
    @ConditionalOnClass(value = {DruidProperties.StatView.class})
    public ServletRegistrationBean<StatViewServlet> registrationBean() {
        // 创建注册器
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>();
        // 创建数据源监控的Servlet
        StatViewServlet servlet = new StatViewServlet();
        // 注入注册器
        registrationBean.setServlet(servlet);
        // 注入相关参数
        registrationBean.addInitParameter("loginUsername",druidProperties.getStatView().getLoginUsername());
        registrationBean.addInitParameter("loginPassword",druidProperties.getStatView().getLoginPassword());
        registrationBean.addInitParameter("allow",druidProperties.getStatView().getAllow());
        registrationBean.addInitParameter("deny",druidProperties.getStatView().getDeny());

        if(druidProperties.getStatView().getUrlMapping()==null || druidProperties.getStatView().getUrlMapping().length==0) {
            log.error("监控的urlMapper不能为空");
            throw new RuntimeException("监控的urlMapper不能为空");
        }
        // 设置映射路径
        registrationBean.addUrlMappings(druidProperties.getStatView().getUrlMapping());
        return registrationBean;
    }
}
